package org.lanqiao.tjut.control;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.lanqiao.tjut.bean.AdminBean;
import org.lanqiao.tjut.myUtils.MyUtils;
import org.lanqiao.tjut.service.ServiceFactory;

public class InsertAdminInfo extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		// // 接收客户端数据请求时进行编码设置，防止中文乱码
		// request.setCharacterEncoding("UTF-8");

		// 1.获取表单数据 2.并将数据填充到实体类中
		// 获取UsersBean类的实例对象
		AdminBean adminBean = (AdminBean) MyUtils.getNewInsdence(AdminBean.class);
		// 以下是获取表单数据
		adminBean.setAdminid(MyUtils.obj2Integer(request.getParameter("inputAdminId")));
		adminBean.setAdminname(request.getParameter("inputAdminName"));
		adminBean.setAdminpsw(request.getParameter("inputPassword3"));
		adminBean.setAdminsex(request.getParameter("inputAdminSex"));
		// 因为年龄为int类型而request.getParameter()获取的为String类型，进行转换
		adminBean.setAdminage(MyUtils.obj2Int(request.getParameter("inputAdminAge")));
		adminBean.setAdmintel(request.getParameter("inputAdminTel"));
		adminBean.setAdminaddress(request.getParameter("inputAdminAdress"));
		adminBean.setAdminideftity(request.getParameter("inputAdminideftity"));

		// 3.Model层进行相应的业务处理
		int i =ServiceFactory.getUserInfoManagerSericeInstance().saveAdminInfo(adminBean);

		// 4.根据业务处理进行view层转发。将显示结果传到客户端
		if (i > 0) {
			// 输入日志
			MyUtils.logger_rootLogger.info("(添加/修改)管理员用户"+adminBean.getAdminname()+"信息成功！");
			// 在跳转之前，将所有用户数据信息集合获取到
			List<AdminBean> lst = ServiceFactory.getUserInfoManagerSericeInstance().getAllAdminInfo();
			// 将需要显示的结果(查询的所有用户信息)存入session缓存中
			request.getSession().setAttribute("lst", lst);

			// 清空request中的缓存（request中的缓存为修改时获取的用户对象信息）
			request.removeAttribute("adminBean");

			// 跳转到相应页面进行显示
			request.getRequestDispatcher("UserInfoManager/ManagerDetailsA.jsp").forward(request, response);
		} else {
			request.getSession().setAttribute("pageErrorInfo", "用户信息加载失败！");
			response.sendRedirect("Logined/Error.jsp");
		}

	}

}
